//
//  TTCalculatorCollectionView.m
//  TaoTuo
//
//  Created by admin on 2020/2/4.
//  Copyright © 2020 admin. All rights reserved.
//

#import "TTCalculatorCollectionView.h"
#import "TTCalculatorViewCell.h"

static NSString *cellID = @"cellID";

@interface TTCalculatorCollectionView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *datasArray;
@property (nonatomic, copy) SelectedBlock selectedBlock;

@end

@implementation TTCalculatorCollectionView

+ (instancetype)calculatorViewWithFrame:(CGRect)frame datas:(NSArray<NSString *> *)datasArray selectedItemBlock:(SelectedBlock)selectedBlock
{
    TTCalculatorCollectionView *view = [[self alloc] initWithFrame:frame];
    view.datasArray = datasArray;
    view.selectedBlock = selectedBlock;
    [view.collectionView reloadData];
    return view;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.datasArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTCalculatorViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.title = self.datasArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *title = self.datasArray[indexPath.row];
    if (self.selectedBlock) {
        self.selectedBlock(title);
    }
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(50, 53 * kScale);
        layout.minimumLineSpacing = 1;
        layout.minimumInteritemSpacing = 1;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[TTCalculatorViewCell class] forCellWithReuseIdentifier:cellID];
        [self addSubview:_collectionView];
    }
    return _collectionView;
}

@end
